package com.lijie.bs_recruit.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lijie.bs_recruit.Util.WrapperUtil;
import com.lijie.bs_recruit.Util.uploadUtil;
import com.lijie.bs_recruit.VO.ResultVO;
import com.lijie.bs_recruit.model.Resume;
import com.lijie.bs_recruit.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李杰
 * @since 2021-03-16
 */
@RestController
@RequestMapping("/resume")
public class ResumeController {
    @Autowired
    ResumeService resumeService;

    @Autowired
    WrapperUtil wrapperUtil;

    @Autowired
    uploadUtil uploadUtil;

    @Value("${web.upload-path}")
    private String path;

    @RequestMapping("/selectByUserId/{userId}")
    public ResultVO selectByUserId(@PathVariable Integer userId){
        ResultVO resultVO = new ResultVO();
        Resume resumeByUserId = resumeService.findResumeByUserId(userId);
        if(resumeByUserId==null){
            resultVO.setMessage("null");
        }else{
            resultVO.setObject(resumeByUserId);
        }
        return resultVO;
    }

    @RequestMapping("/addResume")
    public String addResume(@RequestBody Resume resume){
        String result="";
        boolean save = resumeService.save(resume);
        result=save?"succ":"error";
        return result;
    }

    @RequestMapping("/updateResume")
    public String updateResume(@RequestBody Resume resume){
        String result="";
        UpdateWrapper<Resume> resumeUpdateWrapper = wrapperUtil.resumeUpdateWrapper(resume);
        Boolean aBoolean = resumeService.update(resumeUpdateWrapper);
        if(aBoolean){
            result="succ";
        }else{
            result="error";
        }
        return result;
    }

    //用户上传头像
    @RequestMapping("/headerImg")
    public String headerImg(MultipartFile file){
        String s = null;
        try {
            s = uploadUtil.filesUpload(file, path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }
}

